package com.taomu.app.zongchouhuli.http.utls;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

public abstract class AbsResponseBodyObservable extends Observable<String> {

    private Observable<ResponseBody> mResponseBodyObservable;

    public AbsResponseBodyObservable(Observable<ResponseBody> mResponseBodyObservable) {
        this.mResponseBodyObservable = mResponseBodyObservable;
    }

    @Override
    protected void subscribeActual(Observer<? super String> observer) {
        mResponseBodyObservable
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(ResponseBody mResponseBody) throws Exception {
                        return mResponseBody.string();
                    }
                })
                .map(new Function<String, String>() {
                    @Override
                    public String apply(String mS) throws Exception {
                        return getResponseDataString(mS);
                    }
                })
                .subscribe(observer);
    }

    protected abstract String getResponseDataString(String responseBody) throws Exception;

}
